package com.ux.util
{
	import flash.external.ExternalInterface;

	/**
	 * @author winnie
	 */	
	public class JsUtil
	{
		
		public static function windowOpen(
			url:String, title:String = "", 
			width:Number = 0, height:Number = 0, 
			top:Number = 0, left:Number = 0, 
			toolbar:String = "yes", location:String = "yes",
			status:String = "yes", menubar:String = "yes", 
			scrollbars:String = "yes", resizable:String = "yes"
		
		) : void
		{
			var param : String = "";
			
			if( width != 0 ) param = param + "width="+String( width ) + ",";
			if( height != 0 ) param = param + "height=" + String( height ) + ",";
			if( width != 0 ) param = param + "width="+String( width ) + ",";
			if( height != 0 ) param = param + "height=" + String( height ) + ",";
			if( toolbar == "yes" ) param = param + "toolbar=yes" + ",";
			if( location == "yes" ) param = param + "location=yes" + ",";
			if( status == "yes" ) param = param + "status=yes" + ",";
			if( menubar == "yes" ) param = param + "menubar=yes" + ",";
			if( scrollbars == "yes" ) param = param + "scrollbars=yes" + ",";
			if( resizable == "yes" ) param = param + "resizable=yes" + ",";
			
			if( param.charAt( ( param.length - 1 ) ) == "," )
			{
				param = param.substring( 0, param.length-1 );
			}
			
			try{
				ExternalInterface.call("window.open('"+url+"', '"+title+"','"+param+"')");
			}
			catch(e:Error){}
			
			
		}
		
		
		
		/**
		 * @param isViewConfirm "웹 페이지를 닫겠습니까?"
		 */		
		public static function windowClose(isViewConfirm:Boolean=true) : void
		{
			if(isViewConfirm==true)
			{
				ExternalInterface.call("window.close()");
				
			}
			else
			{
				ExternalInterface.call("window.open('about:blank','_self').close()");
			}
		}
		
		
		
		
		/**
		 * @param varName 자바스크립트 내, 외장 변수 이름
		 * @return 자바스크립트 변수 리턴. 
		 */		
		public static function getJsVars( varName : String ):Object
		{
			var vars : Object = ExternalInterface.call( "eval", varName );
			return vars;
		}
		
		
		
		
		public static function link( url:String ):void
		{
			var _fn : String = "window.location.href('"+url+"')";
			ExternalInterface.call( _fn );
		}
		
		
		public static function alert( msg:String ):void
		{
			try
			{
				ExternalInterface.call( "alert", msg );
			}
			catch( e:Error )
			{
				
			}
		}
		
		public function JsUtil()
		{
		}
	}
}